package com.example.test_witns.CustomLayoutManager;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class CustomStaggeredGridLayoutManager extends StaggeredGridLayoutManager {
    public CustomStaggeredGridLayoutManager(int spanCount, int orientation) {
        super(spanCount, orientation);
    }

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
        if (state.getItemCount() == 0) {
            super.onMeasure(recycler, state, widthSpec, heightSpec);
            return;
        }

        int width = View.MeasureSpec.getSize(widthSpec);
        int spanWidth = width / getSpanCount();
        int totalHeight = 0;

        for (int i = 0; i < state.getItemCount(); i++) {
            View view = recycler.getViewForPosition(i);
            if (view != null) {
                measureChildWithMargins(view, 0, 0);
                int height = getDecoratedMeasuredHeight(view);
                totalHeight += height;
            }
        }

        setMeasuredDimension(width, totalHeight);
    }
}